/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DTO;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author Trung
 */
public class MySocket {
    private Socket sk;
    private String ipString;
    //private final int PORT = -1;
    
    private InputStream inStream;
    private OutputStream outStream;
    private BufferedReader bufReader;
    private BufferedWriter bufWriter;
    private ObjectInputStream oiStream;
    private ObjectOutputStream ooStream;
    
    public MySocket() 
    {
        sk = new Socket();
        ipString = "";
    }
    
    public MySocket(Socket _sk, String _ipString)
    {
        try
        {
            sk = _sk;
            ipString = _ipString;   

            inStream = sk.getInputStream();
            outStream = sk.getOutputStream();
            bufReader = new BufferedReader(new InputStreamReader(getInStream()));
            bufWriter = new BufferedWriter(new OutputStreamWriter(getOutStream()));
            oiStream = new ObjectInputStream(getInStream());
            ooStream = new ObjectOutputStream(getOutStream());
            
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }
    
    public boolean connect(int port)
    {
        try
        {
            setSk(new Socket(getIpString(), port));
            return true;
        }
        catch(UnknownHostException ex)
        {
            System.out.println(ex.getMessage());
            return false;
        }
        catch(IOException ioe)
        {
            System.out.println(ioe.getMessage());
            return false;
        }
    }

    /**
     * @return the sk
     */
    public Socket getSk() {
        return sk;
    }

    /**
     * @param sk the sk to set
     */
    public void setSk(Socket sk) {
        this.sk = sk;
    }

    /**
     * @return the ipString
     */
    public String getIpString() {
        return ipString;
    }

    /**
     * @param ipString the ipString to set
     */
    public void setIpString(String ipString) {
        this.ipString = ipString;
    }

    /**
     * @return the inStream
     */
    public InputStream getInStream() {
        return inStream;
    }

    /**
     * @param inStream the inStream to set
     */
    public void setInStream(InputStream inStream) {
        this.inStream = inStream;
    }

    /**
     * @return the outStream
     */
    public OutputStream getOutStream() {
        return outStream;
    }

    /**
     * @param outStream the outStream to set
     */
    public void setOutStream(OutputStream outStream) {
        this.outStream = outStream;
    }

    /**
     * @return the bufReader
     */
    public BufferedReader getBufReader() {
        return bufReader;
    }

    /**
     * @param bufReader the bufReader to set
     */
    public void setBufReader(BufferedReader bufReader) {
        this.bufReader = bufReader;
    }

    /**
     * @return the bufWriter
     */
    public BufferedWriter getBufWriter() {
        return bufWriter;
    }

    /**
     * @param bufWriter the bufWriter to set
     */
    public void setBufWriter(BufferedWriter bufWriter) {
        this.bufWriter = bufWriter;
    }

    /**
     * @return the oiStream
     */
    public ObjectInputStream getOiStream() {
        return oiStream;
    }

    /**
     * @param oiStream the oiStream to set
     */
    public void setOiStream(ObjectInputStream oiStream) {
        this.oiStream = oiStream;
    }

    /**
     * @return the ooStream
     */
    public ObjectOutputStream getOoStream() {
        return ooStream;
    }

    /**
     * @param ooStream the ooStream to set
     */
    public void setOoStream(ObjectOutputStream ooStream) {
        this.ooStream = ooStream;
    }
}
